package com.jingyanzi.marketing.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.marketing.dao.DiscountLogMapper;

import com.jingyanzi.marketing.domain.DiscountLogDo;
import com.jingyanzi.marketing.entity.MaDiscountLog;
import com.jingyanzi.marketing.entity.MaDiscountLogExample;
import com.jingyanzi.marketing.entity.MaDiscountLogExample.Criteria;
import com.jingyanzi.marketing.query.DiscountLogQuery;
import com.jingyanzi.marketing.result.ResultDO;
import com.jingyanzi.marketing.result.ResultSupport;
import com.jingyanzi.marketing.service.IDiscountLogService;
import com.jingyanzi.marketing.util.BeanUtilsExtends;

@Service("maDiscountLogService")
public class DiscountLogServiceImpl extends BaseServiceImpl implements IDiscountLogService {

	@Autowired
	private DiscountLogMapper discountLogMapper;

	public ResultDO add(DiscountLogDo maDiscountLogDo) {
		MaDiscountLog discountLog = new MaDiscountLog();

		ResultSupport result = BeanUtilsExtends.copy(discountLog, maDiscountLogDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		// role.setCode(UUID.randomUUID().toString());
		discountLog.setCreatetime(Calendar.getInstance().getTime());
		discountLog.setIsDelete(1);
		int r = -1;

		try {
			r = discountLogMapper.insert(discountLog);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, discountLog.getId());
		return result;
	}

	public ResultDO modifi(DiscountLogDo maDiscountLogDo) {
		MaDiscountLog discountLog = new MaDiscountLog();
		ResultSupport result = BeanUtilsExtends.copy(discountLog, maDiscountLogDo);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		int r = -1;

		try {
			r = discountLogMapper.updateByPrimaryKeySelective(discountLog);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO remove(Integer id) {
		MaDiscountLog discountLog = new MaDiscountLog();
		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		int r = -1;
		discountLog.setId(id);
		discountLog.setIsDelete(-1);
		try {
			r = discountLogMapper.updateByPrimaryKeySelective(discountLog);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO get(Integer id) {
		MaDiscountLog discountLog = new MaDiscountLog();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			discountLog = discountLogMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		if (discountLog == null) {
			result.setErrorCode(ResultDO.DISCOUNTLOG_GET_ERROR);
			result.setErrorMsg("获取会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}
		DiscountLogDo maDiscountLog = getMaDiscountLogDo(discountLog);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maDiscountLog);
		return result;
	}

	public ResultDO query(DiscountLogQuery maDiscountLogQuery) {
		ResultSupport result = new ResultSupport();

		List<MaDiscountLog> list = null;

		MaDiscountLogExample example = new MaDiscountLogExample();

		Criteria createCriteria = example.createCriteria();
		if (maDiscountLogQuery.getMemberId() != null) {
			createCriteria.andMemberIdEqualTo(maDiscountLogQuery.getMemberId());
		}

		if (maDiscountLogQuery.getVoucherId() != null) {
			createCriteria.andVoucherIdEqualTo(maDiscountLogQuery.getVoucherId());
		}

		createCriteria.andIsDeleteEqualTo(1);

		List<DiscountLogDo> maDiscountLogDos = null;

		if (maDiscountLogQuery.isPage()) {
			example.setLimitByPage(maDiscountLogQuery.getStart() + "," + maDiscountLogQuery.getPageRows());
			long count = discountLogMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = discountLogMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取会员优惠卷记录失败");
			result.setSuccess(false);
			return result;
		}

		maDiscountLogDos = getMaDiscountLogDoList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, maDiscountLogDos);

		return result;
	}

}
